package com.cennavi.bigdata.baotou.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by cennavi on 2017/9/25.
 */
public class RTICMapJCUtil {
    private static JSONObject info;
    private static JSONObject jcdd;
    private static JSONObject other;

    static {
    	String rootPath = RTICMapJCUtil.class.getClassLoader().getResource("").getPath();
    	System.out.println("root path:" + rootPath);
    	File file = new File(rootPath + "/rtic_jc_map.txt");
        StringBuffer sb = new StringBuffer();
//        String sb = "";
        try {
        	BufferedReader reader = new BufferedReader(new FileReader(file));

            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString);
//                sb += tempString;
            }
            reader.close();
            if (sb!=null) {
                String jsonStr = sb.toString();
                JSONObject json = JSONObject.parseObject(jsonStr);
                info = json.getJSONObject("info");
                System.out.println("info：" + info.keySet().size());
                jcdd = json.getJSONObject("jcdd");
                System.out.println("jcdd:" + jcdd.keySet().size());
                other = json.getJSONObject("other");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static JSONObject getJsonObject() {
        return info;
    }

    public static JSONObject getINFO() {
        return info;
    }

    public static JSONObject getJCDD(){
        return jcdd;
    }

    public static JSONObject getOther(){
        return other;
    }

    public static JSONObject getInfo_more(){
    	JSONObject result=new JSONObject();
    	Iterator<String> it=info.keySet().iterator();
    	while(it.hasNext()){
    		String key=it.next();
    		JSONObject temp=info.getJSONObject(key);
    		String jcName=temp.getString("jcName");
    		if(!result.containsKey(jcName)){
    			result.put(jcName, JSONObject.parse("{'主干道':[],'次干道':[]}"));
    		}
    		JSONObject item=result.getJSONObject(jcName);
    		String roadID=key.split(",")[2];
    		String kind=temp.getString("kind");
    		temp.put("roadid", roadID);
    		if(kind.equals("3")||kind.equals("5")){
    			item.getJSONArray("主干道").add(temp);
    		}else{
    			item.getJSONArray("次干道").add(temp);
    		}
    	}
    	return result;
    }
    
    public static JSONObject getInfo_count(){
    	JSONObject result=getInfo_more();
    	Iterator<String> it2=result.keySet().iterator();
    	while(it2.hasNext()){
    		String key =it2.next();
    		JSONObject temp=result.getJSONObject(key);
    		int c1=temp.getJSONArray("主干道").size();
    		int c2=temp.getJSONArray("次干道").size();
    		temp.put("主干道", c1);
    		temp.put("次干道", c2);
    	}
    	return result;
    }
}
